package com.tim.app.ui.view;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.recycle.WrapRecyclerView;
import com.qiyukf.unicorn.api.ConsultSource;
import com.qiyukf.unicorn.api.SavePowerConfig;
import com.qiyukf.unicorn.api.StatusBarNotificationConfig;
import com.qiyukf.unicorn.api.UICustomization;
import com.qiyukf.unicorn.api.Unicorn;
import com.qiyukf.unicorn.api.YSFOptions;
import com.qiyukf.unicorn.api.YSFUserInfo;
import com.tim.app.R;
import com.tim.app.server.entry.Service;
import com.tim.app.server.logic.UserManager;
import com.tim.app.ui.activity.OfficialNoticeActivity;
import com.tim.app.ui.adapter.message.ServiceAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息或者通知详情的header
 */

public class MessageHeadView extends LinearLayout implements BaseRecyclerAdapter.OnItemClickListener {


    private WrapRecyclerView wrvOfficialContacts;
    private ServiceAdapter adapter;
    private List<Service> dataList;

    Service official = new Service();
    Service service = new Service();

    public MessageHeadView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        wrvOfficialContacts = (WrapRecyclerView) findViewById(R.id.wrvOfficialContacts);
        dataList = new ArrayList<>();
        adapter = new ServiceAdapter(getContext(), dataList);
        wrvOfficialContacts.setAdapter(adapter);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        wrvOfficialContacts.setLayoutManager(layoutManager);
        adapter.setOnItemClickListener(this);
        setData();
    }

    public void setData() {
        official.setName("茉莉官方");
        official.setDesc("点我查看官方最新活动及社区指南~");
        official.setId(1);
        official.setUnReadCount(UserManager.ins().getUnreadOfficialMessageCount());
        service.setName("小茉莉");
        service.setDesc("有任何疑问或建议，都可以找我哦~");
        service.setId(2);
        dataList.add(official);
        dataList.add(service);
        adapter.notifyDataSetChanged();
    }

    /**
     * 更新官方通知的数量
     */
    public void updateUnreadOfficialMessageCount() {
        official.setUnReadCount(UserManager.ins().getUnreadOfficialMessageCount());
        dataList.set(0, official);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(View view, int position, long id) {
        if (position == 1) {
            String title = "茉莉";
            /**
             * 设置访客来源，标识访客是从哪个页面发起咨询的，
             * 用于客服了解用户是从什么页面进入三个参数分别为
             * 来源页面的url，来源页面标题，来源页面额外信息（可自由定义）。
             * 设置来源后，在客服会话界面的"用户资料"栏的页面项，可以看到这里设置的值。
             */
            ConsultSource source = new ConsultSource("", title, "custom information string");
            /**
             * 请注意： 调用该接口前，应先检查Unicorn.isServiceAvailable()，
             * 如果返回为false，该接口不会有任何动作
             *
             * @param context 上下文
             * @param title   聊天窗口的标题
             * @param source  咨询的发起来源，包括发起咨询的url，title，描述信息等
             */
            Unicorn.updateOptions(initOptions());
            Unicorn.openServiceActivity(getContext(), title, source);
        } else {
            OfficialNoticeActivity.start(getContext(), false);
        }
    }

    // 如果返回值为null，则全部使用默认参数。
    private YSFOptions initOptions() {
        YSFOptions options = new YSFOptions();
        options.statusBarNotificationConfig = new StatusBarNotificationConfig();
        options.savePowerConfig = new SavePowerConfig();

        UICustomization uiCustomization = new UICustomization();
        uiCustomization.hideKeyboardOnEnterConsult = true;
        uiCustomization.inputTextSize = 15;
        uiCustomization.hideAudio = true;
        uiCustomization.hideEmoji = true;
        uiCustomization.msgBackgroundColor = Color.parseColor("#F9F9F9");
        uiCustomization.titleBackgroundColor = Color.parseColor("#FFFFFF");
        uiCustomization.topTipBarTextColor = Color.parseColor("#454C57");
        uiCustomization.msgItemBackgroundLeft = R.drawable.ic_left_chat_bg;
        uiCustomization.msgItemBackgroundRight = R.drawable.ic_right_chat_bg;
        uiCustomization.textMsgSize = 14;
        uiCustomization.textMsgColorLeft = Color.parseColor("#454C57");
        uiCustomization.textMsgColorRight = Color.parseColor("#454C57");
        uiCustomization.inputTextSize = 14;
        uiCustomization.inputTextColor = Color.parseColor("#454C57");
        uiCustomization.leftAvatar = "http://moli2017.oss-cn-zhangjiakou.aliyuncs.com/user/msg_img_001.png";
        if (UserManager.ins().isLogin()) {
            uiCustomization.rightAvatar = UserManager.ins().getLoginUser().getImage();

            YSFUserInfo userInfo = new YSFUserInfo();
            userInfo.userId = UserManager.ins().getUserId();
            userInfo.data = "[ {\"key\":\"real_name\", \"value\": \"" + UserManager.ins().getLoginUser().getName() + "\"},  {\"key\":\"mobile_phone\", \"hidden\":false,\"value\":\"" + UserManager.ins().getLoginUser().getPhone() + "\"}, {\"key\":\"avatar\", \"value\": \"" + UserManager.ins().getLoginUser().getImage() + "\"}]";
            Unicorn.setUserInfo(userInfo);
        }
        options.uiCustomization = uiCustomization;

        return options;
    }

}
